package com.ruoyi.jihuo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jihuo.domain.JhTmAccount;
import com.ruoyi.jihuo.domain.search.JhTmAccountSearch;
import com.ruoyi.jihuo.service.IJhTmAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 天猫账户Controller
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/jihuo/account")
public class JhTmAccountController extends BaseController {
    @Autowired
    private IJhTmAccountService jhTmAccountService;

    /**
     * 查询天猫账户列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:list')")
    @GetMapping("/list")
    public TableDataInfo list(JhTmAccountSearch JhTmAccount) {
        startPage();
        List<JhTmAccount> list = jhTmAccountService.selectJhTmAccountList(JhTmAccount);
        return getDataTable(list);
    }

    /**
     * 导出天猫账户列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:export')")
    @Log(title = "天猫账户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JhTmAccountSearch jhTmAccount) {
        List<JhTmAccount> list = jhTmAccountService.selectJhTmAccountList(jhTmAccount);
        ExcelUtil<JhTmAccount> util = new ExcelUtil<JhTmAccount>(JhTmAccount.class);
        util.exportExcel(response, list, "天猫账户数据");
    }

    /**
     * 获取天猫账户详细信息
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:query')")
    @GetMapping(value = "/{jhId}")
    public AjaxResult getInfo(@PathVariable("jhId") Long jhId) {
        return success(jhTmAccountService.selectJhTmAccountByJhId(jhId));
    }

    /**
     * 新增天猫账户
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:add')")
    @Log(title = "天猫账户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JhTmAccount jhTmAccount) {
        return toAjax(jhTmAccountService.insertJhTmAccount(jhTmAccount));
    }

    /**
     * 修改天猫账户
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:edit')")
    @Log(title = "天猫账户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JhTmAccount jhTmAccount) {
        return toAjax(jhTmAccountService.updateJhTmAccount(jhTmAccount));
    }

    /**
     * 修改天猫账户
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:edit')")
    @Log(title = "天猫账户", businessType = BusinessType.UPDATE)
    @PutMapping("/updateJhTmAccountConfig")
    public AjaxResult updateJhTmAccountConfig(@RequestBody JhTmAccount jhTmAccount) {
        return toAjax(jhTmAccountService.updateJhTmAccountConfig(jhTmAccount));
    }

    /**
     * 删除天猫账户
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:remove')")
    @Log(title = "天猫账户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jhIds}")
    public AjaxResult remove(@PathVariable Long[] jhIds) {
        return toAjax(jhTmAccountService.deleteJhTmAccountByJhIds(jhIds));
    }

    /**
     * 流量转移
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:edit')")
    @Log(title = "天猫账户", businessType = BusinessType.UPDATE)
    @PostMapping("/transferTraffic")
    public AjaxResult transferTraffic(@RequestBody JSONObject json) {
        return toAjax(jhTmAccountService.transferTraffic(json.getLong("jhId"), json.getLong("originalId")));
    }

    /**
     * 检查授权状态
     */
    @PreAuthorize("@ss.hasPermi('jihuo:account:edit')")
    @Log(title = "天猫账户", businessType = BusinessType.UPDATE)
    @PostMapping("/checkAuthorization/{jhId}")
    public AjaxResult checkAuthorization(@PathVariable("jhId") Long jhId) {
        return toAjax(jhTmAccountService.checkAuthorization(jhId));
    }
}
